import re

# 读取和处理.txt文件
def extract_and_write_ips(input_file, output_file):
    ip_pattern = re.compile(r'\b(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.'
                            r'(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.'
                            r'(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.'
                            r'(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b')

    with open(input_file, 'r') as file:
        with open(output_file, 'w') as outfile:
            for line in file:
                # 查找IP地址
                ip_matches = ip_pattern.findall(line)
                for ip in ip_matches:
                    outfile.write(f"{ip}\n")  # 按顺序写入输出文件

    print(f"提取的IP地址已按顺序保存到 {output_file} 文件中")

# 示例：指定输入和输出文件路径
input_file = 'output_1.txt'
output_file = 'output_ip.txt'

# 调用函数
extract_and_write_ips(input_file, output_file)
